package edu.sjsu.cs286.teodoro.cipresso.playfield;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

import edu.sjsu.cs286.teodoro.cipresso.wizard.BlueTankGameContainer;

/**
 * This class implement a midlet that is a two player P2P tank combat game which
 * is played over a Bluetooth connection. Each phone will run an identical J2ME
 * midlet with each player selecting their tank color at the start of the game.
 * Tanks would vary by color only, though an additional variation could be speed
 * versus weapon damage. Play will be real-time (not turn-based), which requires
 * each phone to continually update a distributed resource such that each player
 * will see a consistent game state.
 * 
 * @author Teodoro Cipresso
 */
public class BlueTankMIDlet extends MIDlet implements CommandListener {

	private BlueTankGameContainer blueTankGameContainer = null;

	/**
	 * Framework method call to give an opportunity to initialize and start the
	 * midlet.
	 */
	public void startApp() {
		// setup new game
		blueTankGameContainer = new BlueTankGameContainer(this);
		blueTankGameContainer.setupNewGame();
		//
	} // startApp()

	/**
	 * We don't allow the game to be paused because it's a network game.
	 */
	public void pauseApp() {
	} // pauseApp()

	/**
	 * Need to close out the game and clean up any network connections.
	 */
	public void destroyApp(boolean unconditional) {
		//
		if (blueTankGameContainer.getBtManager() != null) {
			blueTankGameContainer.getBtManager().closeBtConnection();
		}
		//
		if (blueTankGameContainer.getBtCanvas() != null)
			blueTankGameContainer.getBtCanvas().stop();
		//
		notifyDestroyed();
	} // destroyApp()

	/**
	 * Handle the exit softkey
	 */
	public void commandAction(Command c, Displayable s) {
		if (c.getCommandType() == Command.EXIT) {
			destroyApp(true);
			notifyDestroyed();
		}
	} // commandAction()

	/**
	 * Getter for the GameContainer instance which represents the entire state
	 * and logic of a game instance
	 */
	public BlueTankGameContainer getGameContainer() {
		return blueTankGameContainer;
	} // BlueTankGameContainer()

} // BlueTankMIDlet()
